package com.xoa.util.cms.directive;

import com.xoa.dao.portalArticle.PortalArticleMapper;
import com.xoa.dao.portalColumn.PortalColumnMapper;
import com.xoa.dao.portalTemplate.PortalTemplateMapper;
import com.xoa.dao.cms.SiteMapper;
import com.xoa.model.portalArticle.PortalArticleWithBLOBs;
import com.xoa.model.portalColumn.PortalColumn;
import com.xoa.model.portalTemplate.PortalTemplateWithBLOBs;
import com.xoa.model.cms.Site;
import com.xoa.service.cms.RenderingService;
import com.xoa.util.common.StringUtils;
import freemarker.core.Environment;
import freemarker.template.*;

import java.io.IOException;
import java.io.Writer;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//获取某个栏目下的文章列表
public class CMSDocumentsDirective implements TemplateDirectiveModel{
    @Override
    public void execute(Environment environment, Map map, TemplateModel[] templateModels, TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException {
        //栏目表示串
        String channelIds = StringUtils.getString(map.get("chalIds"));
        //获取前多少条
        int top = StringUtils.getInteger(map.get("top"),0);
        int start = StringUtils.getInteger(map.get("start"),0);

        //排序方式
        String orderBy = StringUtils.getString(map.get("orderBy"));
        //标签

        //是否分页
        String isPage = StringUtils.getString(map.get("page"));
        Writer w = environment.getOut();

        int siteId = StringUtils.getInteger(environment.getCustomAttribute("SITE_ID"),0);

        //获取文章Mapper
        PortalArticleMapper portalArticleMapper = (PortalArticleMapper)environment.getCustomAttribute("portalArticleMapper");
        PortalColumnMapper portalColumnMapper = (PortalColumnMapper) environment.getCustomAttribute("portalColumnMapper");
        SiteMapper siteMapper = (SiteMapper) environment.getCustomAttribute("siteMapper");
        PortalTemplateMapper portalTemplateMapper = (PortalTemplateMapper) environment.getCustomAttribute("portalTemplateMapper");
        RenderingService renderingService = (RenderingService) environment.getCustomAttribute("renderingService");

        //获取站点信息
        Site site = siteMapper.selectByPrimaryKey(siteId);

        //保存上层变量
        Map loopObj = (Map) environment.getCustomAttribute("loopObj");
        TemplateModel model = environment.getVariable("this");
        //获取当前栏目的id
        int channelId = StringUtils.getInteger(environment.getCustomAttribute("channelId"),0);
        //是否预览模式
        boolean preview = (boolean) environment.getCustomAttribute("preview");

        //每页大小
        int pageSize = 0;
        //当前是第几页
        int curPage = StringUtils.getInteger(environment.getCustomAttribute("curPage"),1);

        List<PortalArticleWithBLOBs> docs = null;

        if("".equals(isPage)){
            if("".equals(channelIds)){
                //如果栏目表示为空，则获取当前栏目下的文章
                Map param = new HashMap();
                param.put("columnId",channelId);
                param.put("start",start);
                param.put("size",top==0?Integer.MAX_VALUE:top);
                if(orderBy!=null && !"".equals(orderBy)){
                    param.put("orderBy",orderBy);
                }
                docs = portalArticleMapper.getDocByChannelId(param);
            }else{
                //存在栏目标识，根据栏目表示获取所有文章
                Map param = new HashMap();
                param.put("mark",channelIds);
                param.put("siteId",siteId);
                param.put("start",start);
                param.put("size",top==0?Integer.MAX_VALUE:top);
                if(orderBy!=null && !"".equals(orderBy)){
                    param.put("orderBy",orderBy);
                }
                docs = portalArticleMapper.selectArticeByColumnMarks(param);
            }
        }else{
            //获取栏目的分页大小
            PortalColumn portalColumn = portalColumnMapper.selectByPrimaryKey(channelId);
            pageSize = portalColumn.getPageSize();
            int form = (curPage-1)*pageSize;
            if("".equals(channelIds)){
                //如果栏目表示为空，则获取当前栏目下的文章
                Map param = new HashMap();
                param.put("columnId",channelId);
                param.put("start",form);
                param.put("size",top==0?Integer.MAX_VALUE:top);
                if(orderBy!=null && !"".equals(orderBy)){
                    param.put("orderBy",orderBy);
                }
                docs = portalArticleMapper.getDocByChannelId(param);
            }else{

            }
        }

        int chnLid = 0;
        String absPath = null;
        Integer docId = 0;
        Calendar calendar = Calendar.getInstance();

        if(docs!=null){
            int index0 = 0;
            String relPath = "";
            for(PortalArticleWithBLOBs a : docs){
                Map item = new HashMap();
                chnLid = a.getColumnId();
                //获取模板信息
                PortalTemplateWithBLOBs withBLOBs = portalTemplateMapper.selectTempByArticlId(a.getArticleId());
                //获取文档绝对路径
                absPath = renderingService.getCloumnAbsolutePath(chnLid);
                if(withBLOBs!=null){
                    //磁盘路径+文件名称
                    item.put("ABS_PATH",absPath+"/"+a.getArticleId()+"."+site.getPubFileExt());
                    int index = absPath.indexOf("/",2);
                    absPath = absPath.substring(index+1);
                    item.put("REL_PATH",environment.getCustomAttribute("ROOT_PATH")+absPath+"/"+a.getArticleId()+"."+site.getPubFileExt());
                }else{
                    item.put("ABS_PATH",absPath);
                    int index = absPath.indexOf("/",2);
                    absPath = absPath.substring(index+1);
                    item.put("REL_PATH",environment.getCustomAttribute("ROOT_PAT")+absPath);
                }

                //处理日期
                item.put("writeTimeDesc",a.getAuthorTime());
                item.put("crTimeDesc",a.getCreateTime());


                if(preview){
                    item.put("ABS_PATH","/site/preview?siteId="+site.getPortalId()+"&channelId="+chnLid+"&chnlDocId="+a.getArticleId());
                    item.put("REL_PATH","/site/preview?siteId="+site.getPortalId()+"&channelId="+chnLid+"&chnlDocId="+a.getArticleId());
                }
                //把文档属性拷到item中
                item.put("content",a.getArticleComtent());
                item.put("subHead",a.getSubHead());
                item.put("keyWord",a.getKeyWord());
                item.put("articleSummary",a.getArticleSummary());
                item.put("articleSource",a.getArticleSource());
                item.put("author",a.getAuthor());
                item.put("thumbnailName",a.getThumbnailName());
                item.put("attachmentName",a.getAttachmentName());
                //文章主标题
                item.put("mainHead",a.getMainHead());
                //文章标题
                item.put("title",a.getArticleTitle());
                item.put("createUserName",a.getCreateUserName());



                Map _this = new HashMap();
                _this.put("instace",item);
                _this.put("index",index0);
                _this.put("chnlDocId",a.getArticleId());

                if(index0 == docs.size()-1){
                    _this.put("isLast",true);
                }else{
                    _this.put("isLast",false);
                }

                environment.setVariable("this",new DefaultObjectWrapper().wrap(_this));
                environment.setCustomAttribute("loopObj",item);

                docId = a.getArticleId();
                environment.setCustomAttribute("docId",docId);

                if(templateDirectiveBody!=null){
                    templateDirectiveBody.render(w);
                }

                environment.setCustomAttribute("docId",docId);
                environment.setVariable("this",model);
                environment.setCustomAttribute("loopObj",loopObj);
            }
        }


    }
}
